<?php

class User {

    // --- ATTRIBUTES ---

    /**
     * user ID in database
     * 0 = not registred
     * @var Integer
     */
    private $userID;

    // --- OPERATIONS ---

    /**
     * Constructor of current class
     * check for valid userID and set $userID
     *
     * @param  Integer userID
     */
    function __construct($passedID) {
        global $MySQL_DB;

        if($passedID > 0) {
            // check if user exists
            $res_check_user = execute_query("SELECT ID, nickname FROM %PRE%user WHERE ID = ".$passedID,$MySQL_DB);
            if(!($check_user = $res_check_user->fetch_object())) {
                $passedID = 0;
            }
        }

        $this->userID = $passedID;
    }

    /**
     * Reads usergroups from this user
     *
     * @return Array
     */
    function getAllUsergroups() {
        global $MySQL_DB;
        // add usergroup 0 = all users
        $usergroups = array(0);

        // user not logged in?
        if($this->userID == 0) {
            // then return only usergroup 0 = all users
            return $usergroups;
        }

        // add usergroup 1 = registered users
        $usergroups[] = 1;

        // read usergroups
        $res_user_has_usergroups = execute_query("SELECT usergroups_ID FROM %PRE%user_has_usergroups WHERE user_ID = ".$this->userID,$MySQL_DB);
        while($user_has_usergroups = $res_user_has_usergroups->fetch_object()) {
            $usergroups[] = $user_has_usergroups->usergroups_ID;
        }

        // return finished usergroup list
        return $usergroups;

    }

    /**
     * displays login require message an login form
     */
    public function requireUserLogin() {

        if($this->isUserLogged()) {
            // save current url
            $_SESSION['oldUrl'] = getCurrentURL();
            
        }

    }

    /**
     * Checks if User is logged
     *
     * @return Boolean
     */
    public function isUserLogged() {
        if($this->userID == 0) {
            return FALSE;
        }
        else {
            return TRUE;
        }
    }

    // --- GET/SET METHODS ---

    public function getUserID() {
        return $this->userID;
    }
}

?>